package org.study.redis.api.util.jedis;

import redis.clients.jedis.GeoCoordinate;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.args.GeoUnit;
import redis.clients.jedis.params.*;
import redis.clients.jedis.resps.GeoRadiusResponse;

import java.util.List;
import java.util.Map;

/**
 * Redis 操作工具类：Geo
 * 参考 {@link redis.clients.jedis.commands.GeoCommands}
 *
 * @author doveylovey
 * @email 1135782208@qq.com
 * @date 2022年04月23日
 */
public class RedisGeoUtils {
    private RedisGeoUtils() {
        throw new AssertionError("不能实例化 RedisGeoUtils 工具类");
    }

    public static long geoadd(String key, double longitude, double latitude, String member) {
        long geoadd;
        try (Jedis jedis = SysJedisPool.getResource()) {
            geoadd = jedis.geoadd(key, longitude, latitude, member);
        }
        return geoadd;
    }

    public static long geoadd(String key, Map<String, GeoCoordinate> memberCoordinateMap) {
        long geoadd;
        try (Jedis jedis = SysJedisPool.getResource()) {
            geoadd = jedis.geoadd(key, memberCoordinateMap);
        }
        return geoadd;
    }

    public static long geoadd(String key, GeoAddParams params, Map<String, GeoCoordinate> memberCoordinateMap) {
        long geoadd;
        try (Jedis jedis = SysJedisPool.getResource()) {
            geoadd = jedis.geoadd(key, params, memberCoordinateMap);
        }
        return geoadd;
    }

    public static Double geodist(String key, String member1, String member2) {
        Double geodist = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            geodist = jedis.geodist(key, member1, member2);
        }
        return geodist;
    }

    public static Double geodist(String key, String member1, String member2, GeoUnit unit) {
        Double geodist = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            geodist = jedis.geodist(key, member1, member2, unit);
        }
        return geodist;
    }

    public static List<String> geohash(String key, String... members) {
        List<String> geohash = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            geohash = jedis.geohash(key, members);
        }
        return geohash;
    }

    public static List<String> geohash(String key, List<String> members) {
        List<String> geohash = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            geohash = jedis.geohash(key, members.toArray(new String[members.size()]));
        }
        return geohash;
    }

    public static List<GeoCoordinate> geopos(String key, String... members) {
        List<GeoCoordinate> geopos = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            geopos = jedis.geopos(key, members);
        }
        return geopos;
    }

    public static List<GeoCoordinate> geopos(String key, List<String> members) {
        List<GeoCoordinate> geopos = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            geopos = jedis.geopos(key, members.toArray(new String[members.size()]));
        }
        return geopos;
    }

    public static List<GeoRadiusResponse> georadius(String key, double longitude, double latitude, double radius, GeoUnit unit) {
        List<GeoRadiusResponse> georadius = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            georadius = jedis.georadius(key, longitude, latitude, radius, unit);
        }
        return georadius;
    }

    public static List<GeoRadiusResponse> georadiusReadonly(String key, double longitude, double latitude, double radius, GeoUnit unit) {
        List<GeoRadiusResponse> georadiusReadonly = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            georadiusReadonly = jedis.georadiusReadonly(key, longitude, latitude, radius, unit);
        }
        return georadiusReadonly;
    }

    public static List<GeoRadiusResponse> georadius(String key, double longitude, double latitude, double radius, GeoUnit unit, GeoRadiusParam param) {
        List<GeoRadiusResponse> georadius = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            georadius = jedis.georadius(key, longitude, latitude, radius, unit, param);
        }
        return georadius;
    }

    public static List<GeoRadiusResponse> georadiusReadonly(String key, double longitude, double latitude, double radius, GeoUnit unit, GeoRadiusParam param) {
        List<GeoRadiusResponse> georadiusReadonly = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            georadiusReadonly = jedis.georadiusReadonly(key, longitude, latitude, radius, unit, param);
        }
        return georadiusReadonly;
    }

    public static List<GeoRadiusResponse> georadiusByMember(String key, String member, double radius, GeoUnit unit) {
        List<GeoRadiusResponse> georadiusByMember = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            georadiusByMember = jedis.georadiusByMember(key, member, radius, unit);
        }
        return georadiusByMember;
    }

    public static List<GeoRadiusResponse> georadiusByMemberReadonly(String key, String member, double radius, GeoUnit unit) {
        List<GeoRadiusResponse> georadiusByMemberReadonly = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            georadiusByMemberReadonly = jedis.georadiusByMemberReadonly(key, member, radius, unit);
        }
        return georadiusByMemberReadonly;
    }

    public static List<GeoRadiusResponse> georadiusByMember(String key, String member, double radius, GeoUnit unit, GeoRadiusParam param) {
        List<GeoRadiusResponse> georadiusByMember = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            georadiusByMember = jedis.georadiusByMember(key, member, radius, unit, param);
        }
        return georadiusByMember;
    }

    public static List<GeoRadiusResponse> georadiusByMemberReadonly(String key, String member, double radius, GeoUnit unit, GeoRadiusParam param) {
        List<GeoRadiusResponse> georadiusByMemberReadonly = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            georadiusByMemberReadonly = jedis.georadiusByMemberReadonly(key, member, radius, unit, param);
        }
        return georadiusByMemberReadonly;
    }

    public static long georadiusStore(String key, double longitude, double latitude, double radius, GeoUnit unit, GeoRadiusParam param, GeoRadiusStoreParam storeParam) {
        long georadiusStore;
        try (Jedis jedis = SysJedisPool.getResource()) {
            georadiusStore = jedis.georadiusStore(key, longitude, latitude, radius, unit, param, storeParam);
        }
        return georadiusStore;
    }

    public static long georadiusByMemberStore(String key, String member, double radius, GeoUnit unit, GeoRadiusParam param, GeoRadiusStoreParam storeParam) {
        long georadiusByMemberStore;
        try (Jedis jedis = SysJedisPool.getResource()) {
            georadiusByMemberStore = jedis.georadiusByMemberStore(key, member, radius, unit, param, storeParam);
        }
        return georadiusByMemberStore;
    }

    public static List<GeoRadiusResponse> geosearch(String key, String member, double radius, GeoUnit unit) {
        List<GeoRadiusResponse> geosearch = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            geosearch = jedis.geosearch(key, member, radius, unit);
        }
        return geosearch;
    }

    public static List<GeoRadiusResponse> geosearch(String key, GeoCoordinate coord, double radius, GeoUnit unit) {
        List<GeoRadiusResponse> geosearch = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            geosearch = jedis.geosearch(key, coord, radius, unit);
        }
        return geosearch;
    }

    public static List<GeoRadiusResponse> geosearch(String key, String member, double width, double height, GeoUnit unit) {
        List<GeoRadiusResponse> geosearch = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            geosearch = jedis.geosearch(key, member, width, height, unit);
        }
        return geosearch;
    }

    public static List<GeoRadiusResponse> geosearch(String key, GeoCoordinate coord, double width, double height, GeoUnit unit) {
        List<GeoRadiusResponse> geosearch = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            geosearch = jedis.geosearch(key, coord, width, height, unit);
        }
        return geosearch;
    }

    public static List<GeoRadiusResponse> geosearch(String key, GeoSearchParam params) {
        List<GeoRadiusResponse> geosearch = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            geosearch = jedis.geosearch(key, params);
        }
        return geosearch;
    }

    public static long geosearchStore(String dest, String src, String member, double radius, GeoUnit unit) {
        long geosearchStore;
        try (Jedis jedis = SysJedisPool.getResource()) {
            geosearchStore = jedis.geosearchStore(dest, src, member, radius, unit);
        }
        return geosearchStore;
    }

    public static long geosearchStore(String dest, String src, GeoCoordinate coord, double radius, GeoUnit unit) {
        long geosearchStore;
        try (Jedis jedis = SysJedisPool.getResource()) {
            geosearchStore = jedis.geosearchStore(dest, src, coord, radius, unit);
        }
        return geosearchStore;
    }

    public static long geosearchStore(String dest, String src, String member, double width, double height, GeoUnit unit) {
        long geosearchStore;
        try (Jedis jedis = SysJedisPool.getResource()) {
            geosearchStore = jedis.geosearchStore(dest, src, member, width, height, unit);
        }
        return geosearchStore;
    }

    public static long geosearchStore(String dest, String src, GeoCoordinate coord, double width, double height, GeoUnit unit) {
        long geosearchStore;
        try (Jedis jedis = SysJedisPool.getResource()) {
            geosearchStore = jedis.geosearchStore(dest, src, coord, width, height, unit);
        }
        return geosearchStore;
    }

    public static long geosearchStore(String dest, String src, GeoSearchParam params) {
        long geosearchStore;
        try (Jedis jedis = SysJedisPool.getResource()) {
            geosearchStore = jedis.geosearchStore(dest, src, params);
        }
        return geosearchStore;
    }

    public static long geosearchStoreStoreDist(String dest, String src, GeoSearchParam params) {
        long geosearchStoreStoreDist;
        try (Jedis jedis = SysJedisPool.getResource()) {
            geosearchStoreStoreDist = jedis.geosearchStoreStoreDist(dest, src, params);
        }
        return geosearchStoreStoreDist;
    }
}
